<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/20
 * Time: 17:17
 */

namespace zhtfrontend\controllers;

use common\components\actions\DeleteAction;
use common\components\helpers\Fun;
use common\components\helpers\Tree;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;
use yii\web\Response;
use yii\widgets\ActiveForm;
use zhtfrontend\components\bases\BaseController;
use zhtfrontend\models\datas\Article;
use zhtfrontend\models\datas\Headmaster;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\NavMenu;
use zhtfrontend\models\forms\HeadmasterSearchForm;
use yii\data\Pagination;

class ArticleController extends BaseController
{
    //班主任列表
    public function actionIndex()
    {
        $search_form = new Article();
        if (\Yii::$app->request->isPost) {
            $search_form->load(\Yii::$app->request->get());
        }
        $search_data = array_filter($search_form->attributes);
        $model = Article::find()->orderBy('id DESC');
        if (!empty($search_data['name'])) {
            $model->andWhere(['like', 'name', $search_data['name']]);
        }
        if (!empty($search_data['status'])) {
            $model->andWhere(['status'=>$search_data['status']]);
        }
        if(isset(\Yii::$app->user->identity->role) && \Yii::$app->user->identity->role == 'category'){
           $model->andWhere(['type_id'=>\Yii::$app->user->identity->type_id]);
        }
        $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => 14, 'params' => array_merge($_GET, $search_data)]);
        $data = $model->offset($pages->offset)->limit($pages->limit)->all();
        $data = Fun::asArray($data, ['menu_name','is_top','operate']);
        return $this->render('index', ['model' => $data, 'form' => $search_form, 'pages' => $pages]);
    }

    public function actionCreate()
    {
        //$this->layout = '@app/views/layouts/iframe.php';
        $model = new Article();
        $model->status = 1;
        $model->is_top = 0;
        $model->sort_order = 0;

        $modelnav = NavMenu::find()->where(['status'=>1,'type'=>'news']);
        if(\Yii::$app->user->identity->role === 'type'){
            $modelnav->andWhere(['type_id'=>\Yii::$app->user->identity->type_id]);
        }
        $nav = $modelnav->all();
        $navData = [];
        if($nav != null){
            foreach ($nav as $key=>$val){
                if($val->type_id == 1){
                    $typename = '益智';
                }elseif ($val->type_id == 2){
                    $typename = '音乐';
                }else{
                    $typename = '形体';
                }
                $navData[$val->id] = $val->name.'-'.$typename;
            }
        }
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->admin_id = \Yii::$app->user->id;
            $navmenu = NavMenu::findOne($model->menu_id);
            $model->type_id = $navmenu->type_id;
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['article/index']));
            }
        }
        return $this->render('create', ['model' => $model,'nav'=>$navData]);
    }

    public function actionUpdate()
    {
        $id = \Yii::$app->request->get('id');
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model = Article::findOne($id);
        if ($model === null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        if(isset(\Yii::$app->user->identity->role) && \Yii::$app->user->identity->role == 'category'){
            if($model->type_id != \Yii::$app->user->identity->type_id){
                throw new BadRequestHttpException(\Yii::t('zhtfrontend', '没权限'), 404);
            }
        }
        $modelnav = NavMenu::find()->where(['status'=>1,'type'=>'news']);
        if(\Yii::$app->user->identity->role === 'type'){
            $modelnav->andWhere(['type_id'=>\Yii::$app->user->identity->type_id]);
        }
        $nav = $modelnav->all();
        $navData = [];
        if($nav != null){
            foreach ($nav as $key=>$val){
                if($val->type_id == 1){
                    $typename = '益智';
                }elseif ($val->type_id == 2){
                    $typename = '音乐';
                }else{
                    $typename = '形体';
                }
                $navData[$val->id] = $val->name.'-'.$typename;
            }
        }
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $navmenu = NavMenu::findOne($model->menu_id);
            $model->type_id = $navmenu->type_id;
            if ($model->save(false)) {
                return $this->redirectMessage('修改成功', Fun::url(['article/index']));
            }
        }
        return $this->render('create', ['model' => $model,'nav'=>$navData]);
    }

}